package com.tianbing.adlottery.controller.storeadmin;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.jubao.ejb.business.ProductBusiness;
import com.jubao.ejb.dao.ProductDao;
import com.jubao.ejb.entity.Product;
import com.jubao.ejb.entity.ProductPrice;
import com.tianbing.adlottery.helper.SkinUtils;
import com.tianbing.adlottery.util.ControllerFormBean;
import com.tianbing.adlottery.util.UserSession;

public class EditProductPriceController extends SimpleFormController {
	static Logger logger = Logger.getLogger(EditProductPriceController.class.getName());

	private ProductDao productDao;

	private ProductBusiness productBusiness;

	private SkinUtils skinUtils;

	// ------------------GET Request LifeCycle------------------
	protected Object formBackingObject(HttpServletRequest request) throws Exception {
		ControllerFormBean fb = new ControllerFormBean();
		long prdsid = ServletRequestUtils.getLongParameter(request, "prdsid", -1);
		Product prd = productDao.find(prdsid);
		if (prd != null) {
			UserSession auth = skinUtils.getUserAuthorization(request, null);
			long storeSysId = auth.getManagedStoreSysId();
			ProductPrice pp = productBusiness.findProductPrice(storeSysId, prdsid);
			if (pp == null) {
				pp = new ProductPrice();
				pp.setProduct(prd);
			}
			fb.setProductPrice(pp);
		} else {
			fb.setProductPrice(null);
		}

		return fb;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse, java.lang.Object,
	 *      org.springframework.validation.BindException)
	 */
	protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object cmd,
			BindException ex) throws Exception {
		long prdsid = ServletRequestUtils.getLongParameter(request, "prdsid", -1);
		UserSession auth = skinUtils.getUserAuthorization(request, null);
		long storeSysId = auth.getManagedStoreSysId();

		ModelAndView mv = new ModelAndView(this.getSuccessView() + "?stosid=" + storeSysId);
		ControllerFormBean form = (ControllerFormBean) cmd;
		ProductPrice s2c = form.getProductPrice();
		productBusiness.saveProductPrice(storeSysId, prdsid, s2c);
		return mv;
	}

	protected Map referenceData(HttpServletRequest request) throws Exception {
		Map model = new HashMap();

		UserSession auth = skinUtils.getUserAuthorization(request, null);
		return model;
	}

	public void setSkinUtils(SkinUtils skinUtils) {
		this.skinUtils = skinUtils;
	}

	public static void setLogger(Logger logger) {
		EditProductPriceController.logger = logger;
	}

	public void setProductDao(ProductDao productDao) {
		this.productDao = productDao;
	}

	public void setProductBusiness(ProductBusiness productBusiness) {
		this.productBusiness = productBusiness;
	}

}
